import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:mziot/app/config/constant.dart';
import 'package:mziot/app/data/user_entity.dart';
import 'package:mziot/app/modules/auth/mine_provider.dart';
import 'package:mziot/app/routes/app_pages.dart';
import 'package:mziot/app/tools/global_const.dart';
import 'package:mziot/app/tools/tools.dart';
import 'package:mziot/generated/locales.g.dart';

class AuthRegisterController extends GetxController {
  MineProvider provider = MineProvider();
  //controller
  TextEditingController emailController = TextEditingController();
  TextEditingController codeController = TextEditingController();
  TextEditingController passController = TextEditingController();
  //state
  RxBool codeEnable = false.obs;
  RxBool buttonEnable = false.obs;
  //data
  RxString email = ''.obs;
  RxString code = ''.obs;
  RxString password = ''.obs;
  RxInt time = 61.obs;
  Timer? _timer;
  String? thirdInfo;
  final isSee = true.obs;
  final isCheck = false.obs;

  @override
  void onInit() {
    super.onInit();

    thirdInfo = Get.arguments;
    vaildInfo();
  }

  @override
  void onReady() {
    super.onReady();

    email.subject.listen((element) => vaildInfo());
    code.subject.listen((element) => vaildInfo());
    password.subject.listen((element) => vaildInfo());
  }

  void vaildInfo() {
    if (!email.value.isEmail) {
      codeEnable.value = false;
      buttonEnable.value = false;
      return;
    }
    if (codeEnable.value != true) {
      codeEnable.value = false;
    }

    if (code.value.length != 4) {
      buttonEnable.value = false;
      return;
    }

    if (password.value.length < 8) {
      buttonEnable.value = false;
      return;
    }

    buttonEnable.value = true;
  }

  void takeCode() {
    Get.showOverlay(asyncFunction: () => Future.value(), loadingWidget: loadingWidget);
    if (!GetUtils.isEmail(email.value)) {
      showToast(LocaleKeys.tips_email_format.tr);
      return;
    }
    if (time.value != 61) {
      return;
    }
    _timer?.cancel();
    _timer = null;
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (time.value == 0) {
        _timer?.cancel();
        _timer = null;
        time.value = 61;
        return;
      }
      time.value = time.value - 1;
    });
    provider.takeCode(email.value, 2).then((value) {
      if (!value.isSuccess) {
        _timer?.cancel();
        _timer = null;
        time.value = 61;
      }
      showToast(value.msg ?? '');
    });
  }

  void registerAction() {
    if (!GetUtils.isEmail(email.value)) {
      showToast(LocaleKeys.tips_email_format.tr);
      return;
    }

    if (password.value.isEmpty) {
      showToast(LocaleKeys.tips_enter_pass.tr);
      return;
    }

    if (code.value.isEmpty) {
      showToast(LocaleKeys.tips_enter_code.tr);
      return;
    }

    EasyLoading.show();
    provider.register<UserEntity>(email.value, password.value, code.value).then((value) async {
      if (value.code == 0) {
        String info = json.encode(value.data!.toJson());
        GlobalConst.sharedPreferences?.setString(Constant.kUser, info);
        AppPages.INITIAL();
      } else {
        showToast(value.msg ?? '');
      }
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  void bindAction() {
    if (!GetUtils.isEmail(email.value)) {
      showToast(LocaleKeys.tips_email_format.tr);
      return;
    }

    if (code.value.isEmpty) {
      showToast(LocaleKeys.tips_enter_code.tr);
      return;
    }
    EasyLoading.show();
    provider.bindEmail(email.value, code.value).then((value) async {
      if (value.isSuccess) {
        GlobalConst.sharedPreferences?.setString(Constant.kUser, thirdInfo!);
        AppPages.INITIAL();
      } else {
        showToast(value.msg ?? '');
      }
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  @override
  void onClose() {}
}
